<?php

namespace Admin\Controller;
use Think;
/**
 * Created by wangchao.
 * User: Administrator
 * Date: 2017/11/10 0010
 * Time: 14:05
 */

class SearchController extends CommonController
{
    public function search_list()
    {
        if (IS_GET) {

            $fxd = $_GET['fxd'];
            //根据风险点名称查询出风险点ID
            if ($fxd != '') {

                $danger_id = M('Danger')->where(['name' => $fxd])->getField('id');
                $where1['danger_id'] = $danger_id;
            }
           // echo $danger_id;
       }
        $where1['status']='1';
        $Data = M('Object'); // 实例化Data数据模型
        $count   = $Data->where( $where1)->count();
        $list = $this->lists('Object',$where1, 'object_id asc');

        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
                $list[$k]['object_level_name'] = M('ObjectLevel')->where(['id' => $v['object_level_id']])->getField('name');

// 根据危险源ID object_id 查询该危险源本月，累计巡检次数XX次，未巡检多少次
                //execute返回结果数
                $start = date('Y-m-01 00:00:00');
                $end = date('Y-m-d H:i:s');

                $sqlw= "SELECT push_id  FROM `xc_push_record` WHERE object_id =".$v['object_id']." and `create_time` >= unix_timestamp('".$start."') AND `create_time` <= unix_timestamp('".$end."')";
                $model = M( "push_record" );
               // 本月已经巡检次数count1,
                $list[$k]['count1']  =$model ->execute($sqlw);

        //  本月岗位级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlgw=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=5  and t.status=1)";
                $sqlgw=$sqlgw." and `create_time` >= unix_timestamp('".$start."') AND `create_time` <= unix_timestamp('".$end."') ";
                $gw1 = M( "push_record" );
                // 本月员工巡检次数yg1，未巡检次数yg2,
                $list[$k]['yg1']  =$gw1 ->execute($sqlgw);
                $list[$k]['yg2']  =   date('d') - $list[$k]['yg1']  ;
     //本月班组级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlbz=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=4  and t.status=1)";
                $sqlbz=  $sqlbz." and `create_time` >= unix_timestamp('".$start."') AND `create_time` <= unix_timestamp('".$end."') ";
                $bz1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['bz1']  =$bz1 ->execute($sqlbz);
                $list[$k]['bz2']  =   date('d') - $list[$k]['bz1']  ;
     //本月车间级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlcj=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=3  and t.status=1)";
                $sqlcj=$sqlcj." and `create_time` >= unix_timestamp('".$start."') AND `create_time` <= unix_timestamp('".$end."') ";
                $cj1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['cj1']  =$cj1 ->execute($sqlcj);
                $list[$k]['cj2']  = 4 - $list[$k]['cj1']  ;
       //本月专业级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlzy=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=2  and t.status=1)";
                $sqlzy=$sqlzy." and `create_time` >= unix_timestamp('".$start."') AND `create_time` <= unix_timestamp('".$end."') ";
                $zy1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['zy1']  =$zy1 ->execute($sqlzy);
                $list[$k]['zy2']  = 3 - $list[$k]['zy1']  ;
     //  本月公司级别级巡检次数count_gs1 ，公司级应每月巡检1次
              $sqlgs=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=1  and t.status=1)";
                $sqlgs=$sqlgs." and `create_time` >= unix_timestamp('".$start."') AND `create_time` <= unix_timestamp('".$end."') ";
                $gs1 = M( "push_record" );
                     // 本月公司巡检次数count1,
                    $list[$k]['gs1']  =$gs1 ->execute($sqlgs);
                $list[$k]['gs2']  = 1 - $list[$k]['gs1']  ;

                $list[$k]['count2']  =   $list[$k]['gs2']+ $list[$k]['zy2'] +    $list[$k]['cj2']+ $list[$k]['yg2']+  $list[$k]['bz2'];

            }
        }




        $this->assign('list', $list);
        $Page     = new \Think\Page($count, 20);
        $show      = $Page->show();
        $this->assign('page', $show);

        $this->display('search_list');
    }

    public function search_wxy_list()
    {
        if (IS_GET) {

            $datemin = I('datemin', 0, 'strtotime');
            $datemax = I('datemax', 0, 'strtotime');
            $phone = I('phone');
            $workshop = I('workshop');
            $part = I('part');
            $danger = I('danger');
            $is_danger = I('is_danger');
            $status = I('status');

            $wxyID = $_GET['object_id'];
            //根据危险源id，查询，危险源名称的，风险点名称。
            $weixianyuan=  M('Object')->where(['object_id' => $wxyID])->getField('name');
            $fxdID=M('Object')->where(['object_id' => $wxyID])->getField('danger_id');
            $fengxiandian=M('Danger')->where(['id' => $fxdID])->getField('name');
            $sql = "select  r.*,d.id,d.suggest  from xc_push_record r left join  xc_deal_info d on   r.push_id= d.push_id  where    r.object_id=" . $wxyID ;

            //上报创建时间查询
            if ($datemin != 0 && $datemax != 0) {
              $sql=$sql." and r.create_time  between  ".$datemin."  and ". $datemax;
            //    $where['create_time'] = array('between', array($datemin, $datemax));
            }
            if ($phone != '') {
                $sql=$sql."  and  r.push_user_id = ".M('User')->where(['phone'=>$phone])->getField('user_id');
               // $where['push_user_id'] = M('User')->where(['phone'=>$phone])->getField('user_id');
            }
            if ($danger != '') {
                $sql=$sql." and r.danger_id=". M('Danger')->where(['name'=>$danger])->getField('id');;
              //  $where['danger_id'] = M('Danger')->where(['name'=>$danger])->getField('id');
            }
            //是否隐患产讯
            if ($is_danger !== '') {
                $sql=$sql." and r.is_danger=".$is_danger;
             //   $where['is_danger'] = $is_danger;
            }





            $user_id = $_SESSION['user_id']; //用户登录
            //链接查询
          //  $sql = "select  r.*,d.id,d.push_id,d.suggest   from xc_push_record  r,xc_deal_info d where  r.push_id= d.push_id  and     r.object_id=" . $wxyID . " ORDER BY  r.create_time desc ";


            $sql=$sql."  ORDER BY  r.create_time desc ";

            $Data1 = M('push_record');
            $list = $Data1->query($sql);
            $count = 0;
            //$count
            // $list = $this->lists('PushRecord', $where, 'push_id desc');
            foreach ($list as $k => $v) {
                $list[$k]['push_user_name'] = M('User')->where(['user_id' => $v['push_user_id']])->getField('username');
                $list[$k]['deal_user_name'] = M('User')->where(['user_id' => $v['deal_user_id']])->getField('username');
                $list[$k]['pending_user_name'] = M('User')->where(['user_id' => $v['pending_user_id']])->getField('username');
                $list[$k]['object_name'] = M('Object')->where(['object_id' => $v['object_id']])->getField('name');

                //销毁以下指定的变量
                unset($list[$k]['push_user_id']);
                unset($list[$k]['deal_user_id']);
                unset($list[$k]['pending_user_id']);
                $list[$k]['img_url'] = C('ADDR') . $v['img_url'];
                $sug1 = M('DealInfo')->where(['level_id' => 4, 'push_id' => $v['push_id']])->getField('suggest');
                $sug2 = M('DealInfo')->where(['level_id' => 3, 'push_id' => $v['push_id']])->getField('suggest');
                $sug3 = M('DealInfo')->where(['level_id' => 2, 'push_id' => $v['push_id']])->getField('suggest');
                $sug4 = M('DealInfo')->where(['level_id' => 1, 'push_id' => $v['push_id']])->getField('suggest');
                if (!empty($sug1)) {
                    $list[$k]['sug1'] = $sug1;
                } else {
                    $list[$k]['sug1'] = '';
                }
                if (!empty($sug2)) {
                    $list[$k]['sug2'] = $sug2;
                } else {
                    $list[$k]['sug2'] = '';
                }
                if (!empty($sug3)) {
                    $list[$k]['sug3'] = $sug3;
                } else {
                    $list[$k]['sug3'] = '';
                }
                if (!empty($sug4)) {
                    $list[$k]['sug4'] = $sug4;
                } else {
                    $list[$k]['sug4'] = '';
                }

                $count++;
            }
            $this->assign('fengxiandian', $fengxiandian);
            $this->assign('weixianyuan', $weixianyuan);
            $this->assign('list', $list);
            $this->assign('count', $count);
        }
        $this->display('search_wxy_list');

    }
}